#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        if (beginWord == endWord) return 1;
        unordered_map<string, bool> hash;
        for (auto& e : wordList) hash[e] = true;
        if (hash[endWord] == false) return 0;
        int ret = 1, len = beginWord.size();
        queue<string> q;
        q.push(beginWord);
        while (q.size())
        {
            ret++;
            int sz = q.size();
            while (sz--)
            {
                string cur = q.front(); q.pop();
                for (int i = 0; i < len; i++)
                {
                    string tmp = cur;
                    for (int j = 0; j < 26; j++)
                    {
                        tmp[i] = j + 'a';
                        if (hash[tmp])
                        {
                            if (tmp == endWord) return ret;
                            hash[tmp] = false;
                            q.push(tmp);
                        }
                    }
                }
            }
        }
        return 0;
    }
};